/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.umum.msi;
import cn.umum.msi.listener.MSIAdapter;
/**
 *
 * @author Administrator
 */
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSessions;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.ui.dwr.Util;

/**
 * A server-side clock that broadcasts the server time to any browsers that will
 * listen.
 * This is an example of how to control clients using server side threads
 * @author Joe Walker [joe at getahead dot ltd dot uk]
 */
public class MSI implements Runnable {

    /**
     * Create a schedule to update the clock every second.
     */
    public MSI() {
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        executor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS);
    }

    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {

        if (isactived) {
            count = MSIAdapter.getCount();
            if (count == -1) {
                //setContent("socketCloseed"+ new Date().toString());
                //log.debug("Sent message");
                isactived = false;
            } else if (count == -8) {
                setClockDisplay("ringingStopped");
            } else if (count == -2) {
                setClockDisplay("call income");
            } else if (count == -13) {
                setClockDisplay("dialoutDTMFInform");
            } else {
                setClockDisplay(new Date().toString());
            }
        } else {
            setClockDisplay("msi stopped,please connect at once");
        }
    }

    /**
     * Called from the client to turn the clock on/off
     */
    public synchronized void toggle() {
        isactived = !isactived;

        if (isactived) {
            setClockDisplay("Started");
        }
    //msiStart();
    }

    /**
     * Actually alter the clients.
     * In DWR 2.x you had to know the ServletContext in order to be able to get
     * a ServerContext. With DWR 3.0 this restriction has been removed.
     * This method is public so you can call this from the dwr auto-generated
     * pages to demo altering one page from another
     * @param output The string to display.
     */
    public void setClockDisplay(final String output) {
        String page = ServerContextFactory.get().getContextPath() + "/reverseajax/clock.html";
        Browser.withPage(page, new Runnable() {

            public void run() {
                Util.setValue("clockDisplay", output);
                Util.removeAllOptions("users");
                //Util.addOptions("users", MSIAdapter.callimcomeusers, "callimcomeusers");
		        Util.addOptions("users", MSIAdapter.agentStats.values(), "userid");
                //ScriptSessions.addFunctionCall("receiveMessages", MSIAdapter.agentStats.values());
            }
        });
    }
    /**
     * Are we updating the clocks on all the pages?
     */
    protected transient boolean isactived = false;
    private int count;
    //public static List<User> users = MSIAdapter.users;
}